using System.Runtime.CompilerServices;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

public class GenericDialog : MonoBehaviour
{
	public delegate void GenericDialogEventHandler();

	private RectTransform rectTransform_;

	private CanvasGroup canvasGroup;

	[HideInInspector]
	public bool isBusy;

	private Vector3 initialScale = Vector3.one;

	[HideInInspector]
	public float alpha = 1f;

	private Tweener tweenAlpha;

	private Tweener tweenScale;

	private Tweener tweenColor;

	public RectTransform rectTransform
	{
		get
		{
			if (rectTransform_ == null)
			{
				rectTransform_ = GetComponent<RectTransform>();
			}
			return rectTransform_;
		}
	}

	[method: MethodImpl(32)]
	public static event GenericDialogEventHandler DialogEnabledEvent;

	[method: MethodImpl(32)]
	public static event GenericDialogEventHandler DialogFinishedUpdatingEvent;

	public static void FireDialogEnabledEvent()
	{
		if (GenericDialog.DialogEnabledEvent != null)
		{
			GenericDialog.DialogEnabledEvent();
		}
	}

	public static void FireDialogFinishedUpdatingEvent()
	{
		if (GenericDialog.DialogFinishedUpdatingEvent != null)
		{
			GenericDialog.DialogFinishedUpdatingEvent();
		}
	}

	private void Awake()
	{
		canvasGroup = GetComponent<CanvasGroup>();
		initialScale = rectTransform.localScale;
	}

	public virtual void Start()
	{
		if (canvasGroup == null)
		{
			canvasGroup = GetComponent<CanvasGroup>();
		}
		if (canvasGroup != null)
		{
			SetAlpha(canvasGroup.alpha);
		}
		UpdateActiveState();
	}

	public void DoAnimationPulseScale(Graphic target, float fromScale = 1.5f, float toScale = 1f, float duration = 0.25f)
	{
		StopTween(tweenScale);
		target.rectTransform.localScale = Vector3.one * fromScale;
		tweenScale = DOTween.To(() => target.rectTransform.localScale, delegate(Vector3 x)
		{
			target.rectTransform.localScale = x;
		}, Vector3.one * toScale, duration).SetEase(Ease.OutCubic);
	}

	public void DoAnimationPulseColor(Graphic target, Color fromColor, Color toColor, float duration = 0.25f)
	{
		StopTween(tweenColor);
		target.color = fromColor;
		tweenColor = DOTween.To(() => target.color, delegate(Color x)
		{
			target.color = x;
		}, toColor, duration).SetEase(Ease.OutCubic);
	}

	private void StopTween(Tweener tween)
	{
		if (tween != null)
		{
			tween.Kill();
			tween = null;
		}
	}

	public void FadeTo(float targetAlpha, float duration)
	{
		base.gameObject.SetActive(true);
		tweenAlpha = DOTween.To(SetAlpha, alpha, targetAlpha, duration).SetEase(Ease.InSine).SetDelay(0.03f)
			.OnComplete(UpdateActiveState);
	}

	private void KillTweenAlpha()
	{
		if (tweenAlpha != null)
		{
			tweenAlpha.Kill();
			tweenAlpha = null;
		}
	}

	public void FadeIn(bool withSound = true, float duration = 0.08f)
	{
		FadeTo(1f, duration);
		if (withSound)
		{
			Audio.PlaySoundWindowOpen();
		}
	}

	public void FadeOut(bool withSound = true, float duration = 0.05f)
	{
		FadeTo(0f, duration);
		if (withSound)
		{
			Audio.PlaySoundWindowClose();
		}
	}

	public virtual void Show()
	{
		SetAlpha(1f);
		UpdateActiveState();
	}

	public virtual void Hide()
	{
		SetAlpha(0f);
		UpdateActiveState();
	}

	private void SetAlpha(float targetAlpha)
	{
		alpha = targetAlpha;
		canvasGroup.alpha = alpha;
		rectTransform.localScale = initialScale * alpha;
	}

	private void UpdateActiveState()
	{
		base.gameObject.SetActive(alpha > 0.0001f);
	}
}
